package com.workcell.workdata.xthings.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.workcell.workdata.xthings.constant.HardwareStateEnum;
import com.workcell.workdata.xthings.dto.CodeChainnumVo;
import com.workcell.workdata.xthings.entity.BasePowermeter;
import com.workcell.workdata.xthings.entity.CodeChainnum;
import com.workcell.workdata.xthings.entity.CodePowermeter;
import com.workcell.workdata.xthings.mapper.CodePowermeterMapper;
import com.workcell.workdata.xthings.service.BasePowermeterService;
import com.workcell.workdata.xthings.service.CodePowermeterService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 功率计二维码
 *
 * @author 廖博文
 * @date 2022-11-28 10:15:25
 */
@Service
public class CodePowermeterServiceImpl extends ServiceImpl<CodePowermeterMapper, CodePowermeter> implements CodePowermeterService{

	@Resource
	private BasePowermeterService basePowermeterService;

	/**
	 * 保存二维码基本快照信息
	 * @param codeChainnum
	 * @return
	 */
	@Override
	public Boolean saveCodeBaseInfo(CodeChainnum codeChainnum, CodeChainnumVo.SaveChainNumVo saveChainNumVo) {
		// 查询硬件信息
		LambdaQueryWrapper<BasePowermeter> basePowermeterCondition = new QueryWrapper<BasePowermeter>().lambda()
				.eq(BasePowermeter::getPmId,codeChainnum.getBaseId());
		BasePowermeter basePowermeter = basePowermeterService.getOne(basePowermeterCondition);
		if (ObjectUtil.isEmpty(basePowermeter)){
			return false;
		}
		// 添加二维码快照信息
		CodePowermeter saveBean = new CodePowermeter();
		BeanUtil.copyProperties(basePowermeter,saveBean,"createBy","createTime","updateBy","updateTime");
		saveBean.setCodeId(codeChainnum.getCodeId());
		saveBean.setChainNum(codeChainnum.getChainNum());
		saveBean.setDeviceNo(codeChainnum.getDeviceNo());
		saveBean.setBaudRate(saveChainNumVo.getBaudRate());
		saveBean.setCommDataFormat(saveChainNumVo.getCommDataFormat());
		return this.save(saveBean);
	}

	/**
	 * 获取硬件清单
	 * @return
	 */
	@Override
	public List<JSONObject> getHardwareList() {
		LambdaQueryWrapper<BasePowermeter> lambdaQueryWrapper = Wrappers.lambdaQuery(BasePowermeter.class);
		lambdaQueryWrapper.eq(BasePowermeter::getState, HardwareStateEnum.INUSE.getCode());
		return basePowermeterService.list(lambdaQueryWrapper).stream().map(i -> {
			JSONObject resultBean = new JSONObject();
			resultBean.put("baseId",i.getPmId());
			resultBean.put("manufacturer",i.getManufacturer());
			resultBean.put("hardwareName",i.getPmName());
			return resultBean;
		}).collect(Collectors.toList());
	}

	/**
	 * 查询硬件基本信息
	 * @param baseIds
	 * @return
	 */
	@Override
	public Object getHardwareBase(String baseIds) {
		List<String> baseList = Arrays.asList(baseIds.split(","));
		LambdaQueryWrapper<BasePowermeter> basePowermeterCondtion = new QueryWrapper<BasePowermeter>().lambda()
				.in(CollectionUtil.isNotEmpty(baseList),BasePowermeter::getPmId,baseList);
		List<BasePowermeter> baseFlowmeterList = basePowermeterService.list(basePowermeterCondtion);
		return baseFlowmeterList;
	}

	/**
	 * 查询硬件快照信息
	 * @param codeId
	 * @return
	 */
	@Override
	public Object getHardwareInfo(String codeId) {
		return this.getById(codeId);
	}

}
